package behavioralPattern.observer.example03;

import behavioralPattern.observer.example02.LotteryResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事件处理类 - 处理类之间关系
 */
public class EventManager {

    public enum EventType{
        Message,
        MQ
    }

    // 监听器集合
    Map<Enum<EventType>, List<EventListener>> map = new HashMap<>();

    public EventManager(Enum<EventType>... operations) {
        for (Enum<EventType> operation : operations) {
            this.map.put(operation,new ArrayList<>());
        }
    }


    // 订阅
    public void subscribe(Enum<EventType> eventTypeEnum,EventListener listener){
        List<EventListener> listeners = map.get(eventTypeEnum);
        listeners.add(listener);
    }

    // 取消订阅
    public void unSubscribe(Enum<EventType> eventTypeEnum,EventListener listener){
        List<EventListener> listeners = map.get(eventTypeEnum);
        listeners.remove(listener);
    }

    // 通知
    public void notifySubject(Enum<EventType> eventTypeEnum, LotteryResult result){
        List<EventListener> eventListeners = map.get(eventTypeEnum);
        for (EventListener eventListener : eventListeners) {
            eventListener.doEvent(result);
        }
    }
}
